package com.example.android.util;

import android.text.TextUtils;

import com.example.android.db.City;
import com.example.android.db.County;
import com.example.android.db.Province;

import org.json.JSONArray;
import org.json.JSONObject;

public class Utility {
    public static boolean handleProvinceResponse(String response){
        if (!TextUtils.isEmpty(response)){
            try{
                JSONArray allProvinces = new JSONArray(response);
                for (int i = 0;i < allProvinces.length();i++){
                    JSONObject provinceObject = allProvinces.getJSONObject(i);
                    Province province = new Province();
                    province.setProvinceName(provinceObject.getString("name"));
                    province.setProvinceCode(provinceObject.getInt("id"));
                    province.save();
                }
                return true;
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
        return false;
    }
    public static boolean handleCityResponse(String respon,int provinceId){
        if (!TextUtils.isEmpty(respon)){
            try{
                JSONArray allCities = new JSONArray(respon);
                for (int i = 0;i < allCities.length();i++);
                City city = new City();
                city.setProvinceId(provinceId);
                city.setCityCode(cityObject.getInt("id"));
                city.setCityName(cityObjcet.getString("name"));
                city.save();
            }
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return false;
    }
    public static boolean handleCountResponse(String response,int cityId){
        if (!TextUtils.isEmpty(response)){
            try {
                JSONArray allCounties = new JSONArray(response);
                for (int i = 0;i<allCounties.length();i++){
                    JSONObject countyObject.getJSONobject(i);
                    County county=new County();
                    county.setCountyName(countyObject.getString("name"));
                    county.setWeatherId(countyObject.getString("weather_id"));
                    county.setCityId(cityId);
                    county.save();
                }
            }
        }
    }
}
